home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / SpriteFight 2002 v2.0a1 / Application.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  24.9 KB  |  1,176 lines  |  [TEXT/KAHL]

  1. //    Application.c
  2. //    by Stefan C. Sinclair Copyright © 1995-1996 All rights reserved.
  3.  
  4. #include "Application.h"
  5.  
  6. Boolean gIsRunning, gQT, gSoundOff, gSoundMax, gSoundMegaMax, gSpeech;
  7. Boolean gSecretCode[kNumSecretCodes];
  8. SndChannelPtr gSoundChanP1, gSoundChanP2, gSoundChanGame;
  9. PixPatHandle gLifePixPatH;
  10. CWindowPtr    gWindowP;
  11. short    gFrameAdvanceTime, gPlayer1Controller, gPlayer2Controller, gFileLoaded[3];
  12. SpriteFileRec    gPlayer1File, gPlayer2File;
  13. CombatZoneFileRec gCombatZoneFile;
  14.  
  15. void main(void)
  16. {
  17.     OSErr err = noErr;
  18.     Boolean appOK = FALSE;
  19.     
  20.     if (Initialize())
  21.     {
  22.         OpeningSplash();
  23.         SetGlobals();
  24.         if(CheckSystem())
  25.         {
  26.             err = InstallAppleEventHandlers();
  27.             if (err != noErr)
  28.                 ErrorAlert(err, kUnknownErrorStringIndex);
  29.         }
  30.         else
  31.             ExitToShell();
  32.             
  33.         CreateMenuBar();
  34.         appOK = EnterApplication();
  35.         if (appOK)
  36.         {
  37.             GetStefanStuff();
  38.             EventLoop();
  39.         }
  40.     }
  41.     ExitApplication();
  42. }
  43.  
  44. void OpeningSplash(void)
  45. {
  46.     short ok;
  47.     Handle    soundH;
  48.     SndChannelPtr musicChan;
  49.     
  50.     ErrorSound(nil);
  51.     // Get the music
  52.     soundH = Get1Resource('snd ', kOpeningSoundResID);
  53.     if(soundH == NULL)
  54.         CantFindResource();
  55.     MoveHHi(soundH);
  56.     HLock(soundH);
  57.     InitSoundChannel(&musicChan);
  58.     PlaySound(soundH, musicChan);
  59.     
  60.     ok = Alert(kIntroSplashAlertID, NULL);
  61.     
  62.     // Turn off the music
  63.     StopPlayingSound(musicChan);
  64.     HUnlock(soundH);
  65.     ReleaseResource(soundH);
  66.     KillSoundChannel(&musicChan);
  67. }
  68.  
  69.  
  70. void SetGlobals(void)
  71. {    
  72.     short i;
  73.     
  74.     gIsRunning = TRUE, gQT = FALSE, gSpeech = FALSE;
  75.     gSoundOff = FALSE, gSoundMax = FALSE, gSoundMegaMax = FALSE;
  76.     gFileLoaded[0] = gFileLoaded[1] = gFileLoaded[2] = FALSE;
  77.     gFrameAdvanceTime = 9; // 50% of full speed
  78.     gPlayer1Controller = kPlayer1Sprite;
  79.     //gPlayer2Controller = kComputer2Sprite;
  80.     gPlayer2Controller = kPlayer2Sprite;
  81.     SetToZero((Ptr)(&gPlayer1File), sizeof(SpriteFileRec));
  82.     SetToZero((Ptr)(&gPlayer2File), sizeof(SpriteFileRec));
  83.     SetToZero((Ptr)(&gCombatZoneFile), sizeof(SpriteFileRec));
  84.     for(i=0; i<kNumSecretCodes; i++)
  85.         gSecretCode[i] = FALSE;
  86. }
  87.  
  88. void GetStefanStuff(void)
  89. {
  90.     gLifePixPatH = GetPixPat(kLifePixPatID);
  91.     if (gLifePixPatH == NULL)
  92.         CantFindResource();
  93.     DetachResource((Handle)gLifePixPatH);
  94.     
  95.     // Randomize the computer's clock for later useage...
  96.     GetDateTime((unsigned long *)&qd.randSeed);
  97. }
  98.  
  99. void InitSoundChannel(SndChannelPtr *gChan)
  100. {
  101.     OSErr    myErr;
  102.     SndChannelPtr sChan;
  103.     
  104.     sChan = (SndChannelPtr)NewPtrClear(sizeof(SndChannel));
  105.     if(!sChan)
  106.         DoError(kGeneralMemoryError, TRUE);
  107.     sChan->qLength = stdQLength;     // 128 sound commands
  108.     myErr = SndNewChannel(&sChan, sampledSynth, initMono+initNoInterp, nil);
  109.     if(myErr != noErr)
  110.         DoError(kCouldNotCreateASoundChannel, TRUE);
  111.     
  112.     *gChan = sChan;
  113. }
  114.  
  115. void KillSoundChannel(SndChannelPtr *gChan)
  116. {
  117.     OSErr    myErr;
  118.     SndCommand    command;
  119.     SndChannelPtr    sChan = *gChan;
  120.     
  121.     if(sChan == NULL)
  122.         return;
  123.     
  124.     while(SndChannelBusy(sChan))
  125.     {
  126.         command.cmd = flushCmd;
  127.         command.param1 = command.param2 = 0;
  128.         myErr = SndDoImmediate( sChan, &command);
  129.         if(myErr)
  130.             DoError(kCouldNotFlushSoundChannel, TRUE);
  131.         command.cmd = quietCmd;
  132.         command.param1 = command.param2 = 0;
  133.         myErr = SndDoImmediate( sChan, &command);
  134.         if(myErr)
  135.             DoError(kCouldNotQuietSoundChannel, TRUE);
  136.     }
  137.     
  138.     myErr = SndDisposeChannel(sChan, TRUE);
  139.     if(myErr != noErr)
  140.         DoError(kBadSoundChannel, TRUE);
  141.     DisposePtr((Ptr)sChan);
  142.     myErr = MemError();
  143.     if(myErr != noErr)
  144.         DoError(kGeneralMemoryError, TRUE);
  145.     *gChan = NULL;
  146.     
  147. }
  148.  
  149. void SetToZero(void *theAddr, Size numBytes)
  150. {
  151.     Ptr        aPtr;
  152.     Size    i;
  153.     
  154.     aPtr = (Ptr)theAddr;
  155.     for(i=0; i<numBytes; i++)
  156.         aPtr[i] = 0;
  157. }
  158.  
  159. void Ceremonies(void)
  160. {
  161.     Handle textH, soundH;
  162.     short textID = 128;            //• rsrc ID# for text used.
  163.     short styleID = 128;        //• rsrc ID# for styl used.
  164.     long systemTime, delayTime=6;
  165.     StScrpHandle styleHdl;
  166.     TEHandle textHand;
  167.     Rect txtRect;
  168.     Boolean haveEvent;
  169.     EventRecord event;
  170.     SndChannelPtr musicChan;
  171.     short scrollSpeed;
  172.     
  173.     //• Uncomment this line for a beta test release.
  174.     //DoMessage(kBetaNotice);
  175.     
  176.     // Some text to display
  177.     //• Create styled TERecord.
  178.     txtRect = gWindowP->portRect;
  179.     //InsetRect(&txtRect, 5, 1);
  180.     textHand = TEStylNew(&txtRect, &txtRect);
  181.     TESetJust (1, textHand);
  182.     //• Read the TEXT resource.
  183.     textH = GetResource('TEXT', textID);    
  184.     HLock(textH);                    //• Lock handle.
  185.     //• Get the style handle.
  186.     styleHdl = (StScrpHandle)(Get1Resource('styl', styleID));    
  187.     TEStylInsert(*textH, SizeResource(textH), styleHdl, textHand); //• move text into text record.
  188.     HUnlock(textH);                    //• Unlock handle.
  189.     // Set the pens
  190.     ForeColor(whiteColor);
  191.     BackColor(blackColor);
  192.     
  193.     TEUpdate(&txtRect, textHand); //• Draw text in viewRect.
  194.     //InsetRect(&txtRect, -5, -1);
  195.     // Get the music
  196.     soundH = Get1Resource('snd ', kTextSoundResID);
  197.     if(soundH == NULL)
  198.         CantFindResource();
  199.     MoveHHi(soundH);
  200.     HLock(soundH);
  201.     InitSoundChannel(&musicChan);
  202.     
  203.     while(!Button())
  204.     {
  205.         if(isPressed(keyOption)) // if option is down, then go fast
  206.         {
  207.             scrollSpeed = 2;
  208.         }
  209.         else
  210.         {
  211.             Delay(delayTime, &systemTime); // wait a bit
  212.             scrollSpeed = 1;
  213.         }    
  214.         if(isPressed(keyShift))
  215.             scrollSpeed *= 1; // scroll down, instead of up
  216.         else
  217.             scrollSpeed *= -1; // scroll up
  218.         TEPinScroll(0, scrollSpeed, textHand);
  219.         LoopSound(soundH, musicChan);
  220.         haveEvent = WaitNextEvent(everyEvent, &event, 0, NULL); // Let menu clock update!
  221.         //••••••••••• This would be a REALLY GOOD place to hide a startup code!!!!!!!!!!!!!!!!!
  222.     }
  223.     // when done
  224.     TEDispose(textHand);
  225.     // Turn off the music
  226.     StopPlayingSound(musicChan);
  227.     HUnlock(soundH);
  228.     ReleaseResource(soundH);
  229.     KillSoundChannel(&musicChan);
  230.     
  231.     // Reset the pens
  232.     ForeColor(blackColor);
  233.     BackColor(whiteColor);
  234.     RefreshMainWindow();
  235. }
  236.  
  237. Boolean Initialize(void)
  238. {
  239.     OSErr err = noErr;
  240.     EventRecord tempEvent;
  241.  
  242.     if (err == noErr)
  243.     {
  244.         MaxApplZone();
  245.         MoreMasters();
  246.         err = MemError();
  247.     }
  248.     if (err == noErr)
  249.     {
  250.         InitGraf(&qd.thePort);
  251.         InitFonts();
  252.         InitWindows();
  253.         InitMenus();
  254.         TEInit();
  255.         InitDialogs(0L);
  256.         InitCursor();
  257.         FlushEvents(everyEvent, 0);
  258.     }
  259.     else
  260.         ExitToShell();
  261.  
  262.     return err == noErr;
  263. }
  264.  
  265. Boolean CheckSystem(void)
  266. {
  267.     OSErr    err;
  268.     Boolean isSystemGood = true;
  269.     long    gestaltResult, longVersion;
  270.     NumVersion *sndMgrVersion;
  271.  
  272.     // Check for Time Mgr (System 7.0 or higher)
  273.     err = Gestalt(gestaltTimeMgrVersion, &gestaltResult);
  274.     isSystemGood = (err == noErr) && (gestaltResult >= gestaltStandardTimeMgr);
  275.     if (!isSystemGood)
  276.         CantRunOnThisMachine(kNeedSystem7, TRUE);
  277.     
  278.     // Check forColor QuickDraw
  279.     if(!SWHasColorQuickDraw())
  280.         CantRunOnThisMachine(kNeedColorQuickDraw, TRUE);
  281.     else
  282.         isSystemGood = TRUE;
  283.     
  284.     // Check for Sound Mgr 3.0 or better
  285.     isSystemGood = TrapAvail(_SoundDispatch);
  286.     if (!isSystemGood)
  287.         CantRunOnThisMachine(kNeedSoundMgr3, TRUE);
  288.     else
  289.     {
  290.         longVersion = SndSoundManagerVersion();
  291.         sndMgrVersion = (NumVersion *)&longVersion;
  292.         if(sndMgrVersion->majorRev >= 3)
  293.             isSystemGood = TRUE;
  294.         else
  295.             CantRunOnThisMachine(kNeedSoundMgr3, TRUE);
  296.     }
  297.     // Check for QuickTime™
  298.     err = Gestalt( gestaltQuickTime, &gestaltResult );
  299.     if (err != noErr)
  300.     {
  301.       gQT = FALSE;
  302.       DoError(kNoQuickTimeInstalled, FALSE);
  303.     }
  304.     else
  305.     {
  306.         err = EnterMovies();
  307.         if (err != noErr)
  308.              CantRunOnThisMachine(kNeedQuickTime, TRUE);
  309.         else
  310.         {
  311.             isSystemGood = TRUE;
  312.             gQT = TRUE;
  313.             RDQTConflictCheck(); // If QT present, check for RamDoubler conflict.
  314.         }
  315.     }
  316.     
  317.     // Check for Speech Manager
  318.     if(Gestalt( gestaltSpeechAttr, &gestaltResult ) == noErr)
  319.     {
  320.         if(BitTst(&gestaltResult, 31 - gestaltSpeechMgrPresent))
  321.         {
  322.                isSystemGood = TRUE; // Good anyway; just checking for fun.
  323.                gSpeech = TRUE;
  324.            }
  325.            else
  326.                gSpeech = FALSE;
  327.        }
  328.        
  329.     // Check for MacsBug
  330.     if(MacsBugInstalled())
  331.         isSystemGood = TRUE; // Good anyway; just checking for fun.
  332.       
  333.     return isSystemGood;
  334. }
  335.  
  336.  
  337. #ifndef NewAEEventHandlerProc
  338. #define NewAEEventHandlerProc(x) ((EventHandlerProcPtr)x)
  339. #endif 
  340.  
  341. OSErr InstallAppleEventHandlers(void)
  342. {
  343.     OSErr err = noErr;
  344.  
  345.     err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOpenApp), 0, false);
  346.     if (err == noErr)
  347.         err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOpenDoc), 0, false);
  348.     if (err == noErr)
  349.         err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePrintDoc), 0, false);
  350.     if (err == noErr)
  351.         err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
  352.     return err;
  353. }
  354.  
  355.  
  356. void CreateMenuBar(void)
  357. {
  358.     Handle menuBarH;
  359.     MenuHandle    specialMenu;
  360.     int        notANormalMenu = -1;
  361.  
  362.     menuBarH = GetNewMBar(kMenuBarResID);
  363.  
  364.     if (menuBarH != NULL)
  365.     {
  366.         SetMenuBar(menuBarH);
  367.         AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  368.         
  369.         // Add sub-menus
  370.         specialMenu = GetMenu(kP1ControlMenuID);
  371.         InsertMenu(specialMenu, notANormalMenu);
  372.         specialMenu = GetMenu(kP2ControlMenuID);
  373.         InsertMenu(specialMenu, notANormalMenu);
  374.         specialMenu = GetMenu(kAdjustSpeedMenuID);
  375.         InsertMenu(specialMenu, notANormalMenu);
  376.         
  377.         DrawMenuBar();
  378.     }
  379.     else
  380.         CantFindResource();
  381. }
  382.  
  383. Boolean EnterApplication(void)
  384. {
  385.     OSErr    err;
  386.     Rect    windRect;
  387.     short    mBarHeight;
  388.  
  389.     err = SWEnterSpriteWorld();
  390.  
  391.     if (err == noErr)
  392.     {
  393.         mBarHeight = LMGetMBarHeight();
  394.         windRect.top =  mBarHeight + mBarHeight; // so window title bar can be seen!
  395.         // Check for big enough monitor
  396.         if((qd.screenBits.bounds.right<640)||(qd.screenBits.bounds.bottom<480))
  397.             CantRunOnThisMachine(kNeed14inchMonitor, FALSE);
  398.         windRect.left = 0;
  399.         windRect.right = 640;
  400.         windRect.bottom = 480;
  401.         gWindowP = (CWindowPtr)GetNewCWindow(kWindowResID, nil, (WindowPtr)(-1L));
  402.         if (gWindowP == NULL)
  403.             CantFindResource();
  404.         SetPort((GrafPtr)gWindowP);
  405.     }
  406.     if (err == noErr)
  407.     {
  408.         ShowWindow((WindowPtr)gWindowP);
  409.         InvalRect(&gWindowP->portRect);
  410.     }
  411.     if (err != noErr)
  412.         ErrorAlert(err, kUnknownErrorStringIndex);
  413.     return err == noErr;
  414. }
  415.  
  416.  
  417. void ExitApplication(void)
  418. {
  419.     Handle    gameOver;
  420.     OSErr    err;
  421.     Rect    overRect;
  422.     PicHandle    overPict;
  423.     
  424.     overRect.top = overRect.left = 0;
  425.     overRect.bottom = 78;
  426.     overRect.right = 253;
  427.     MyCenterRect(&overRect, &gWindowP->portRect);
  428.     
  429.     FillRect(&gWindowP->portRect, &qd.black);
  430.     overPict = GetPicture(kGameOverManPictID);
  431.     if(overPict == nil)
  432.         DoError(kResError, TRUE);
  433.     HLock((Handle)overPict);
  434.     DrawPicture(overPict, &overRect);
  435.     HUnlock((Handle)overPict);
  436.     ReleaseResource((Handle)overPict);
  437.     
  438.     if(!isPressed(keySpace))
  439.     {
  440.         gameOver = Get1Resource('snd ', kGameOverManSndID);
  441.         if(gameOver == NULL)
  442.             ExitToShell();
  443.         HLock(gameOver);
  444.         err = SndPlay( nil, (SndListHandle)gameOver, FALSE);
  445.         HUnlock(gameOver);
  446.         ReleaseResource(gameOver);
  447.     }
  448.     
  449.     ExitToShell();
  450. }
  451.  
  452.  
  453. void EventLoop(void)
  454. {
  455.     Boolean haveEvent;
  456.     EventRecord event;
  457.  
  458.     while (gIsRunning)
  459.     {
  460.         haveEvent = WaitNextEvent(everyEvent, &event, kForeGroundSleepTime, NULL);
  461.         
  462.         if (haveEvent)
  463.         {
  464.             DispatchEvent(&event);
  465.         }
  466.         else
  467.         {
  468.             HandleNullEvent();
  469.         }
  470.     }
  471. }
  472.  
  473.  
  474. void DispatchEvent(EventRecord* event)
  475. {
  476.     switch(event->what)
  477.     {
  478.         case mouseDown:
  479.             HandleMouseEvent(event);
  480.             break;
  481.         case mouseUp:
  482.             break;
  483.         case keyUp:
  484.             break;
  485.         case keyDown:
  486.         case autoKey:
  487.             HandleKeyEvent((char)(event->message & charCodeMask), event->modifiers);
  488.             break;
  489.         case updateEvt:
  490.             HandleUpdateEvent((WindowPtr)event->message);
  491.             break;
  492.         case diskEvt:
  493.             break;
  494.         case activateEvt:
  495.             break;
  496.         case networkEvt:
  497.             break;
  498.         case driverEvt:
  499.             break;
  500.         case app1Evt:
  501.             break;
  502.         case app2Evt:
  503.             break;
  504.         case app3Evt:
  505.             break;
  506.         case osEvt:
  507.             break;
  508.         case kHighLevelEvent:
  509.             AEProcessAppleEvent(event);
  510.             break;
  511.         default:
  512.             break;
  513.     }
  514. }
  515.  
  516.  
  517. void HandleMouseEvent(EventRecord* event)
  518. {
  519.     WindowPtr whichWindow;
  520.     short partCode;
  521.  
  522.     partCode = FindWindow(event->where, &whichWindow);
  523.  
  524.     switch (partCode)
  525.     {
  526.         case inDesk:        
  527.             break;
  528.         case inMenuBar:
  529.             AdjustMenuItems();
  530.             HandleMenuCommand(MenuSelect(event->where));
  531.             break;
  532.         case inSysWindow:
  533.             SystemClick(event, whichWindow);
  534.             break;
  535.         case inContent:
  536.             break;
  537.         case inDrag:
  538.             DragWindow(whichWindow, event->where, &qd.screenBits.bounds);
  539.             break;
  540.         case inGrow:
  541.             break;
  542.         case inGoAway:
  543.             if (whichWindow == (GrafPort *)gWindowP && 
  544.                     TrackGoAway((GrafPort *)gWindowP, event->where))
  545.                   gIsRunning = (ChoiceAlert(kWantToQuit) == TRUE ? FALSE : TRUE);
  546.             break;
  547.         case inZoomIn:
  548.         case inZoomOut:
  549.             break;
  550.         default:            
  551.             break;
  552.     }
  553. }
  554.  
  555.  
  556. void HandleKeyEvent(char key,short modifiers)
  557. {
  558.     if ((modifiers & cmdKey) != 0)
  559.     {
  560.         AdjustMenuItems();
  561.         HandleMenuCommand(MenuKey(key));
  562.     }
  563.     else // Secret codes! Cool!
  564.     {
  565.         MainLoopCodeCheck(key);
  566.     }
  567. }
  568.  
  569.  
  570. void HandleUpdateEvent(WindowPtr updateWindowP)
  571. {    
  572.     if (updateWindowP == (WindowPtr)gWindowP)
  573.     {
  574.         SetPort(updateWindowP);
  575.         BeginUpdate(updateWindowP);
  576.  
  577.         RefreshMainWindow();
  578.  
  579.         EndUpdate(updateWindowP);
  580.     }
  581. }
  582.  
  583. void RefreshMainWindow(void)
  584. {
  585.     PicHandle    BackGroundPict;
  586.     
  587.     BackGroundPict = GetPicture(kBackGroundPICTResID);
  588.         if(BackGroundPict == nil)
  589.     CantFindResource();
  590.     HLock((Handle)BackGroundPict);
  591.     DrawPicture(BackGroundPict, &gWindowP->portRect);
  592.     HUnlock((Handle)BackGroundPict);
  593.     ReleaseResource((Handle)BackGroundPict);
  594. }
  595.  
  596.  
  597. void HandleNullEvent(void)
  598. {
  599.     return;
  600. }
  601.  
  602.  
  603. void HandleMenuCommand(long menuItemIdentifier)
  604. {
  605.     short menuIdent = HiWord(menuItemIdentifier);
  606.     short menuItem = LoWord(menuItemIdentifier);
  607.  
  608.     switch (menuIdent)
  609.     {
  610.         case kAppleMenuID:
  611.             HandleAppleMenuCommand(menuItem);
  612.             break;
  613.         case kFileMenuID:
  614.             HandleFileMenuCommand(menuItem);
  615.             break;
  616.         case kCombatMenuID:
  617.             HandleCombatMenuCommand(menuItem);
  618.             break;
  619.         case kP1ControlMenuID:
  620.             HandleP1ControlMenuCommand(menuItem);
  621.             break;
  622.         case kP2ControlMenuID:
  623.             HandleP2ControlMenuCommand(menuItem);
  624.             break;
  625.         case kAdjustSpeedMenuID:
  626.             HandleAdjustSpeedMenuCommand(menuItem);
  627.             break;
  628.         case kHiddenMenuID:
  629.             HandleHiddenMenuCommand(menuItem);
  630.             break;
  631.         default:
  632.             break;
  633.     }
  634.     HiliteMenu(0);
  635. }
  636.  
  637.  
  638. void HandleAppleMenuCommand(short menuItem)
  639. {
  640.     Str255 deskAccName;
  641.  
  642.     switch (menuItem)
  643.     {
  644.         case kAboutItem:
  645.             Ceremonies();
  646.             break;
  647.         case kInstructionsItem:
  648.             Helper(kInstructionsPICTResID);
  649.             break;
  650.         case kHintsItem    :
  651.             Helper(kHintsPICTResID);
  652.             break;
  653.         case kMoreItem:
  654.             Helper(kMorePICTResID);
  655.             break;
  656.         default:
  657.             GetItem(GetMHandle(kAppleMenuID), menuItem, deskAccName);
  658.             OpenDeskAcc(deskAccName);
  659.             break;
  660.     }
  661. }
  662.  
  663.  
  664. void HandleFileMenuCommand(short menuItem)
  665. {
  666.     QNodePtr gHeadPtr=NULL, gTailPtr=NULL;
  667.     KNodePtr ggHeadPtr=NULL, ggTailPtr=NULL;
  668.     OSErr    err;
  669.     SpriteFileRec    tournamentPlayer;
  670.     CombatZoneFileRec    tournamentZone;
  671.     Str255        oldWTitle = "\p";
  672.     Boolean    hitOK = TRUE, player1Victorious;
  673.     
  674.     switch (menuItem)
  675.     {
  676.         case kManoAManoItem:
  677.             DoMessage(kSelectPlayer1);
  678.             gFileLoaded[kPlayer1Sprite] = GetMySpriteFile(&gPlayer1File);
  679.             if(gFileLoaded[kPlayer1Sprite])
  680.             {
  681.                 DoMessage(kSelectPlayer2);
  682.                 gFileLoaded[kPlayer2Sprite] = GetMySpriteFile(&gPlayer2File);
  683.             }
  684.             if(gFileLoaded[kPlayer2Sprite])
  685.             {
  686.                 DoMessage(kSelectCombatZone);
  687.                 gFileLoaded[kArena] = GetMyArenaFile(&gCombatZoneFile);
  688.             }
  689.             if(gFileLoaded[kArena])
  690.             {
  691.                 FillRect(&gWindowP->portRect, &qd.black);
  692.                 GetWTitle((WindowPtr)gWindowP, oldWTitle);
  693.                 SetWTitle((WindowPtr)gWindowP, gCombatZoneFile.sfFile.name);
  694.                 err = CombatCommand(&player1Victorious);
  695.                 if(err == noErr)
  696.                 {
  697.                     SetWTitle((WindowPtr)gWindowP, oldWTitle);
  698.                     gFileLoaded[kPlayer1Sprite] = FALSE;
  699.                     gFileLoaded[kPlayer2Sprite] = FALSE;
  700.                     gFileLoaded[kArena] = FALSE;
  701.                 }
  702.                 if(gSpeech && !gSoundOff)
  703.                 {
  704.                     switch(player1Victorious)
  705.                     {
  706.                         case TRUE:
  707.                             err = SpeakString("\pPlayer 1 Wins!");
  708.                             while(SpeechBusy())
  709.                                 ;
  710.                             break;
  711.                         case FALSE:
  712.                             err = SpeakString("\pPlayer 2 Wins!");
  713.                             while(SpeechBusy())
  714.                                 ;
  715.                             break;
  716.                     }
  717.                 }
  718.             }
  719.             RefreshMainWindow();
  720.             break;
  721.         case kTournamentItem:
  722.             DoMessage(kFeatureNotAvailable);
  723.             return; // get rid of this when computer control is fixed up.
  724.             break;
  725.         case kQuitItem:
  726.             gIsRunning = (ChoiceAlert(kWantToQuit) == TRUE ? FALSE : TRUE);
  727.             break;
  728.     }
  729. }
  730.  
  731.  
  732. void HandleCombatMenuCommand(short menuItem)
  733. {
  734.     OSErr err = noErr;
  735.     Boolean player1Victorious;
  736.     Str255 oldWTitle = "\p";
  737.     long    gestaltResult;
  738.     
  739.     switch(menuItem)
  740.     {
  741.         case kSpeechOnOffItem:
  742.             if(gSpeech)
  743.             {
  744.                 gSpeech = FALSE;
  745.                 DoMessage(kSpeechOff);
  746.             }
  747.             else
  748.             {
  749.                 // Check for Speech Manager
  750.                 if(Gestalt( gestaltSpeechAttr, &gestaltResult ) == noErr)
  751.                 {
  752.                     if(BitTst(&gestaltResult, 31 - gestaltSpeechMgrPresent))
  753.                     {
  754.                            gSpeech = TRUE;
  755.                            DoMessage(kSpeechOn);
  756.                        }
  757.                        else
  758.                            gSpeech = FALSE;
  759.                    }
  760.             }
  761.             break;
  762.         case kQuickTimeOnOffItem:
  763.             if(gQT)
  764.             {
  765.                 gQT = FALSE;
  766.                 DoMessage(kQTOff);
  767.             }
  768.             else
  769.             {
  770.                 // Check for QuickTime™
  771.                 err = Gestalt( gestaltQuickTime, &gestaltResult );
  772.                 if (err == noErr)
  773.                 {
  774.                       err = EnterMovies();
  775.                     if (err == noErr)
  776.                     {
  777.                          gQT = TRUE;
  778.                         DoMessage(kQTOn);
  779.                     }
  780.                 }
  781.             }
  782.             break;
  783.         default:
  784.             break;
  785.     }
  786. }
  787.  
  788.  
  789. void HandleP1ControlMenuCommand(short menuItem)
  790. {
  791.     switch (menuItem)
  792.     {
  793.         case kHumanControlItem:
  794.             gPlayer1Controller = kPlayer1Sprite;
  795.             break;
  796.         case kComputerControlItem:
  797.             //gPlayer1Controller = kComputer1Sprite;
  798.             DoMessage(kFeatureNotAvailable);
  799.             break;
  800.     }
  801. }
  802.  
  803. void HandleP2ControlMenuCommand(short menuItem)
  804. {
  805.     switch (menuItem)
  806.     {
  807.         case kHumanControlItem:
  808.             gPlayer2Controller = kPlayer2Sprite;
  809.             break;
  810.         case kComputerControlItem:
  811.             //gPlayer2Controller = kComputer2Sprite;
  812.             DoMessage(kFeatureNotAvailable);
  813.             break;
  814.     }
  815. }
  816.  
  817. void HandleAdjustSpeedMenuCommand(short menuItem)
  818. {
  819.     gFrameAdvanceTime = (menuItem-1);
  820. }
  821.  
  822. void HandleHiddenMenuCommand(short menuItem)
  823. {
  824.     OSErr    myErr;
  825.     
  826.     switch(menuItem)
  827.     {
  828.         case kThisIsHiddenMenuItem:
  829.             if(gSpeech && !gSoundOff)
  830.             {
  831.                 myErr = SpeakString("\pSurprise");
  832.                 while(SpeechBusy())
  833.                     ;
  834.             }
  835.             break;
  836.         case kSoundOffItem:
  837.             gSoundMax = FALSE;
  838.             gSoundMegaMax = FALSE;
  839.             gSoundOff = TRUE;
  840.             break;
  841.         case kMaxSoundItem:
  842.             gSoundOff = FALSE;
  843.             gSoundMegaMax = FALSE;
  844.             gSoundMax = TRUE;
  845.             break;
  846.         case kMegaMaxSoundItem:
  847.             gSoundOff = FALSE;
  848.             gSoundMax = FALSE;
  849.             gSoundMegaMax = TRUE;
  850.         case kNormalSoundItem:
  851.             gSoundOff = FALSE;
  852.             gSoundMax = FALSE;
  853.             gSoundMegaMax = FALSE;
  854.             break;
  855.     }
  856. }
  857.  
  858. void AdjustMenuItems(void)
  859. {
  860.     MenuHandle tempMenuH;
  861.     short    numItems, i;
  862.  
  863.     // adjust the Speed menu
  864.     tempMenuH = GetMHandle(kAdjustSpeedMenuID);
  865.     numItems = CountMItems(tempMenuH);
  866.     for(i=1; i<=numItems; i++)
  867.         CheckItem(tempMenuH, i, FALSE);
  868.     CheckItem(tempMenuH, (gFrameAdvanceTime+1), TRUE);
  869.     
  870.     // Adjust sprite control menus
  871.     // Player 1
  872.     tempMenuH = GetMHandle(kP1ControlMenuID);
  873.     numItems = CountMItems(tempMenuH);
  874.     for(i=1; i<=numItems; i++)
  875.         CheckItem(tempMenuH, i, FALSE);
  876.     switch(gPlayer1Controller)
  877.     {
  878.         case kPlayer1Sprite:
  879.             CheckItem(tempMenuH, 1, TRUE);
  880.             break;
  881.         case kComputer1Sprite:
  882.             CheckItem(tempMenuH, 2, TRUE);
  883.             break;
  884.         default:
  885.             break;
  886.     }
  887.     // Player 2
  888.     tempMenuH = GetMHandle(kP2ControlMenuID);
  889.     numItems = CountMItems(tempMenuH);
  890.     for(i=1; i<=numItems; i++)
  891.         CheckItem(tempMenuH, i, FALSE);
  892.     switch(gPlayer2Controller)
  893.     {
  894.         case kPlayer2Sprite:
  895.             CheckItem(tempMenuH, 1, TRUE);
  896.             break;
  897.         case kComputer2Sprite:
  898.             CheckItem(tempMenuH, 2, TRUE);
  899.             break;
  900.         default:
  901.             break;
  902.     }
  903. }
  904.  
  905. void enqueue(QNodePtr *headPtr, QNodePtr *tailPtr, QData value)
  906. {
  907.     QNodePtr newPtr;
  908.     
  909.     newPtr = (QNodePtr)NewPtr(sizeof(QNode));
  910.     if(newPtr != NULL)
  911.     {
  912.         newPtr->data = value;
  913.         newPtr->nextPtr = NULL;
  914.         if(isEmpty(*headPtr))
  915.             *headPtr = newPtr;
  916.         else
  917.             (*tailPtr)->nextPtr = newPtr;
  918.         *tailPtr = newPtr;
  919.     }
  920.     else
  921.         DoError(kGeneralMemoryError, TRUE);
  922. }
  923.  
  924. QData dequeue(QNodePtr *headPtr, QNodePtr *tailPtr)
  925. {
  926.     QData    value;
  927.     QNodePtr    tempPtr;
  928.     
  929.     value = (*headPtr)->data;
  930.     tempPtr = *headPtr;
  931.     *headPtr = (*headPtr)->nextPtr;
  932.     if(*headPtr == NULL)
  933.         *tailPtr = NULL;
  934.     DisposePtr((Ptr)tempPtr);
  935.     return value;
  936. }
  937.  
  938. Boolean isEmpty(QNodePtr headPtr)
  939. {
  940.     return headPtr == NULL;
  941. }
  942.  
  943. void Kenqueue(KNodePtr *headPtr, KNodePtr *tailPtr, KData value)
  944. {
  945.     KNodePtr newPtr;
  946.     
  947.     newPtr = (KNodePtr)NewPtr(sizeof(QNode));
  948.     if(newPtr != NULL)
  949.     {
  950.         newPtr->data = value;
  951.         newPtr->nextPtr = NULL;
  952.         if(KisEmpty(*headPtr))
  953.             *headPtr = newPtr;
  954.         else
  955.             (*tailPtr)->nextPtr = newPtr;
  956.         *tailPtr = newPtr;
  957.     }
  958.     else
  959.         DoError(kGeneralMemoryError, TRUE);
  960. }
  961.  
  962. KData Kdequeue(KNodePtr *headPtr, KNodePtr *tailPtr)
  963. {
  964.     KData    value;
  965.     KNodePtr    tempPtr;
  966.     
  967.     value = (*headPtr)->data;
  968.     tempPtr = *headPtr;
  969.     *headPtr = (*headPtr)->nextPtr;
  970.     if(*headPtr == NULL)
  971.         *tailPtr = NULL;
  972.     DisposePtr((Ptr)tempPtr);
  973.     return value;
  974. }
  975.  
  976. Boolean KisEmpty(KNodePtr headPtr)
  977. {
  978.     return headPtr == NULL;
  979. }
  980.  
  981.  
  982. pascal OSErr HandleOpenApp(
  983.     AppleEvent srcAppleEvent,
  984.     AppleEvent replyAppleEvent,
  985.     long refCon)
  986. {
  987. #if MPW
  988. #pragma unused(srcAppleEvent,replyAppleEvent,refCon)
  989. #endif
  990.  
  991.     return noErr;
  992. }
  993.  
  994.  
  995. pascal OSErr HandleOpenDoc(
  996.     AppleEvent srcAppleEvent,
  997.     AppleEvent replyAppleEvent,
  998.     long refCon)
  999. {
  1000. #if MPW
  1001. #pragma unused(srcAppleEvent,replyAppleEvent,refCon)
  1002. #endif
  1003.     return errAEEventNotHandled;
  1004. }
  1005.  
  1006.  
  1007. pascal OSErr HandlePrintDoc(
  1008.     AppleEvent srcAppleEvent,
  1009.     AppleEvent replyAppleEvent,
  1010.     long refCon)
  1011. {
  1012. #if MPW
  1013. #pragma unused(srcAppleEvent,replyAppleEvent,refCon)
  1014. #endif
  1015.     return errAEEventNotHandled;
  1016. }
  1017.  
  1018.  
  1019. pascal OSErr HandleQuit(
  1020.     AppleEvent srcAppleEvent,
  1021.     AppleEvent replyAppleEvent,
  1022.     long refCon)
  1023. {
  1024. #if MPW
  1025. #pragma unused(srcAppleEvent,replyAppleEvent,refCon)
  1026. #endif
  1027.     gIsRunning = false;
  1028.  
  1029.     return noErr;
  1030. }
  1031.  
  1032. short NumToolboxTraps(void)
  1033. {
  1034.     return    (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  1035.                 ?    0x0200 :    0x0400;
  1036. }
  1037.  
  1038.  
  1039. TrapType GetTrapType(short trap)
  1040. {
  1041.     #define TrapMask 0x0800
  1042.  
  1043.     return ((trap & TrapMask) != 0) ? ToolTrap : OSTrap;
  1044. }
  1045.  
  1046.  
  1047. Boolean TrapAvail(short trap)
  1048. {
  1049.     TrapType    tType;
  1050.  
  1051.     tType = GetTrapType(trap);
  1052.     if (tType == ToolTrap)
  1053.         trap = trap & 0x07FF;
  1054.     if (trap >= NumToolboxTraps())
  1055.         trap = _Unimplemented;
  1056.     return NGetTrapAddress(trap, tType) !=
  1057.              NGetTrapAddress(_Unimplemented, ToolTrap);
  1058. }
  1059.  
  1060.  
  1061. Boolean HasWaitNextEvent(void)
  1062. {
  1063.     return TrapAvail(_WaitNextEvent);
  1064. }
  1065.  
  1066. Boolean MacsBugInstalled(void)
  1067. {
  1068. /*
  1069.     How to tell if MacsBug is Installed
  1070.     This is a small snippet of code that can be used to to detect if
  1071.     macsbug is installed or not. NOTE:  This code is intended to only work with
  1072.     version 6.2 of macsbug.  You should refer to your Low Level Debugger's manual
  1073.     for more information on how they install themselves.
  1074.  
  1075.     This code is based on information obtained from the MacsBug Reference.
  1076.     The basic assumptions are that macsbug will install itself in the following
  1077.     manner:
  1078.  
  1079.     If you are running in 24 bit mode, then the high -order byte of MacJmp is a flags
  1080.     byte that contains the following information:
  1081.  
  1082.     Bit    Meaning
  1083.     ---    --------------------------------------
  1084.     7    -   Set if debugger is running
  1085.     6    -   Set if debugger can handle system errors
  1086.     5    -   Set if debugger is installed
  1087.     4     -   Set if debugger can support discipline utility
  1088.  
  1089.     The lower 3 bytes are used to store the address of the debugger's entry point.
  1090.  
  1091.     If you are running in 32-bit mode, the flags byte is moved to address 0xBFF and
  1092.     the long word at MacJmp becomes a full 32-bit address that points to the
  1093.     debugger's entry point..
  1094.  
  1095.     ADDENDUM:  The above information seems to be incorrect in the reference
  1096.     manual. I have found through testing etc. that in both modes, the Flag Byte
  1097.     appears at location 0xBFF.  The code reflects these findings.
  1098.  
  1099. */
  1100.     Ptr FlagByte  = (Ptr)0xBFF;   // This is used only if running in 32 bit mode
  1101.  
  1102.     return (*FlagByte & 0x20/* Used to detect if bit 5 is set */);
  1103.  
  1104. }
  1105.  
  1106. /*
  1107. If your program currently checks Gestalt to see if virtual memory is present, 
  1108. you will need to add a few lines of code to your program to determine if the result 
  1109. of the gestaltVMPresent means that Virtual Memory or RAM Doubler is present.
  1110. The following C code contains two functions. The first, VMActive, checks to see if 
  1111. virtual memory is present. You probably already have similar code in your application,
  1112. but we include the sample here for consistency and completeness. The second function,
  1113. WhichVMIsInstalled, checks to see if RAM Doubler is present.
  1114. */
  1115. /* get some declarations out of the way */
  1116. /*    kNoVM = no virtual memory present                        */
  1117. /*    kSystem7 = System 7 virtual memory manager                */
  1118. /*    kRAMDoubler = RAM Doubler is using VM hook                 */
  1119. enum {
  1120.     kNoVM = 1,
  1121.     kSystem7,
  1122.     kRAMDoubler
  1123. };
  1124.  
  1125. void RDQTConflictCheck(void)
  1126. {
  1127.     Boolean ramDoublerOn;
  1128.     short    itemHit, hitOK=1;
  1129.     
  1130.     ramDoublerOn = (WhichVMIsInstalled() == kRAMDoubler);
  1131.     if(ramDoublerOn)
  1132.     {
  1133.         // Nevermind this - seems to work OK now!
  1134.         //itemHit = CautionAlert(kRDQTCautionAlertID, nil);
  1135.         //if(itemHit == hitOK)
  1136.             //gQT = FALSE;
  1137.     }
  1138. }
  1139.  
  1140. /* TYPICAL VIRTUAL MEMORY CHECK                */
  1141. /* VMActive returns True if virtual memory present    */
  1142. Boolean VMActive(void)
  1143. {
  1144.     long response;
  1145.     
  1146.     return (GetOSTrapAddress(_Gestalt) != GetToolTrapAddress(_Unimplemented)
  1147.             && Gestalt(gestaltVMAttr, &response) == noErr
  1148.             && (response & (1<<gestaltVMPresent)));
  1149. }
  1150.  
  1151. /* —————————————————————————             */
  1152. /* RAM DOUBLER CHECK                                    */
  1153. /* WhichVMIsInstalled tells you what kind of memory you’re dealing with:*/
  1154. /*    kNoVM = no virtual memory present                        */
  1155. /*    kSystem7 = System 7 virtual memory manager                */
  1156. /*    kRAMDoubler = RAM Doubler is using VM hook                 */
  1157. short WhichVMIsInstalled(void)
  1158. {
  1159.     long    vmSignature;
  1160.     if (!VMActive())
  1161.     {
  1162.         return kNoVM;
  1163.     }
  1164.     else
  1165.     {
  1166.         if(Gestalt('vmem', &vmSignature) == noErr && vmSignature == 'RaM2')
  1167.         {
  1168.             return kRAMDoubler;
  1169.         }
  1170.         else
  1171.         {
  1172.             return kSystem7;
  1173.         }
  1174.     }
  1175. }
  1176.